package com.code.monitor.utils;

import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author ggh
 * @date 2023/5/8
 */
public class Md5Utils {
    /**
     * 加密
     *
     * @param text
     */
    public static String encrypt(String text) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            byte[] bytes = text.getBytes();
            byte[] buff = instance.digest(bytes);
            String key = Hex.encodeHexString(buff);
            return key;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加密byte数组
     *
     * @param bytes
     */
    public static String encryptBytes(byte[] bytes) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            byte[] buff = instance.digest(bytes);

            String key = Hex.encodeHexString(buff);
            return key;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
